package main

import (
	"fmt"
	"strings"
)

func nextLine(sb *strings.Builder, deep int) {
	sb.WriteRune('\n')
	for i := 0; i < deep; i++ {
		sb.WriteRune(' ')
	}
}

// 格式化json
func formatJson(json *string) *string {
	var sb strings.Builder
	var nextC uint8
	len := len(*json)
	var deep int
	for index, c := range *json {
		if c == ' ' {
			continue
		}
		sb.WriteRune(c)
		if c == '{' || c == '[' {
			deep++
			nextLine(&sb, deep)
		}
		if c == ',' {
			nextLine(&sb, deep)
		}
		if c == ':' {
			sb.WriteRune(' ')
		}
		if index == len-1 {
			break
		}
		nextC = (*json)[index+1]
		if nextC == '}' || nextC == ']' {
			deep--
			nextLine(&sb, deep)
		}
	}
	var s = sb.String()
	return &s

}

func main43() {

	json := "{\"id\":1, \"name\":\"李四\", \"googFriend\": {\"id\":2, \"name\":\"张三\"}, \"friends\":[{\"id\":3, \"name\":\"王五\"}, {\"id\":4, \"name\": \"陈六\", \"likes\": [\"音乐\", \"电影\", \"游戏\"]}]}"
	s := formatJson(&json)
	fmt.Println(*s)
}
